package cn.edu.scau.cmi.chenruilin.composite.safe;

import java.util.ArrayList;

import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;

public class safeCompositeController {

    @FXML
    private TextField p_in;

    @FXML
    private Label output;

    @FXML
    private TextField o_in;

    @FXML
    private TextField t_in;
    
    @FXML
    private TextField t_in2;

    private static SafeComposite c_sc = new SafeComposite();
	private static SafeComposite p_sc = new SafeComposite();
	private static SafeLeaf s_sc = new SafeLeaf();
	private static int space = 1;
	private static SafeComposite organization = new SafeComposite();
	private static String s;
	
	public static void print(SafeComposite organization, int space) {
		
		ArrayList<SafeComponent> chilren = organization.getChildren();
		for(int i = 0; i < chilren.size(); i++) {
			for(int j = 0; j < space; j++) {
				s += "        ";
			}
			SafeComponent sc = chilren.get(i);
			if(sc instanceof SafeLeaf) {
				SafeLeaf sc2 = (SafeLeaf) sc;
				s += "个人："+sc2.getName()+"\n";
			}
			else if(sc instanceof SafeComposite) {
				SafeComposite sc1 = (SafeComposite) sc;
				s += "团队："+sc1.getName()+"\n";
				if(sc1.getChildren()!=null) {
					print(sc1, space+1);
				}
			}
		}
	}
	
	public static void check(SafeComposite organization, String name) {
		
		ArrayList<SafeComponent> chilren = organization.getChildren();
		
		for(int i = 0; i < chilren.size(); i++) {
			SafeComponent sc = chilren.get(i);
			if(sc instanceof SafeComposite) {
				SafeComposite sc1 = (SafeComposite) sc;
				if(sc1.getName().compareTo(name)==0) {
					c_sc = sc1;
					return;
				}
				if(sc1.getChildren()!=null) {
					check(sc1, name);
				}
			}
			
		}
	}
    
	public static void check2(SafeComposite organization, String name) {
		
		ArrayList<SafeComponent> chilren = organization.getChildren();
		for(int i = 0; i < chilren.size(); i++) {
			SafeComponent sc = chilren.get(i);
			if(((SafeComposite)sc).getName().compareTo(name)==0) {
				p_sc = organization;
			}
			if(sc instanceof SafeLeaf) {
				SafeLeaf sf = (SafeLeaf) sc;
				if(sf.getName().compareTo(name)==0) {
					s_sc = sf;
					return;
				}
			}
			else if(sc instanceof SafeComposite) {
				SafeComposite sc1 = (SafeComposite) sc;
				if(sc1.getName().compareTo(name)==0) {
					c_sc = sc1;
					return;
				}
				if(sc1.getChildren()!=null) {
					
					check2(sc1, name);
				}
			}
			
			
		}
	}
	
    @FXML
    void setOrganization() {
    	c_sc = null;
		p_sc = organization;
		s_sc = null;
    	s = null;
    	if(organization.getName()!=null) {
    		Alert al = new Alert(AlertType.WARNING);
    		al.setTitle("WARNING");
    		al.setHeaderText(null);
    		al.setContentText("只允许有一个组织！");
    		al.showAndWait();
    	}
    	else {
    		String name = o_in.getText();
    		organization.setName(name);
    		s = "组织："+organization.getName()+"\n";
    		output.setText(s);
    	}
    }

    @FXML
    void setPerson() {
    	c_sc = null;
		p_sc = organization;
		s_sc = null;
    	s = null;
    	ArrayList<SafeComponent> chilren = organization.getChildren();
		String p_name = t_in2.getText();
		if(p_name.compareTo(organization.getName()) == 0) {
			String person_name = p_in.getText();
			SafeLeaf person = new SafeLeaf();
			person.setName(person_name);
			organization.addChild(person);
			s = "组织："+organization.getName()+"\n";
			print(organization, space);
		}
		else {
			check(organization, p_name);
			if(c_sc == null) {
				Alert al = new Alert(AlertType.WARNING);
	    		al.setTitle("WARNING");
	    		al.setHeaderText(null);
	    		al.setContentText("请输入正确的组织或团队名称作为父亲节点！");
	    		al.showAndWait();
			}
			else {
				String person_name = p_in.getText();
				SafeLeaf person = new SafeLeaf();
				person.setName(person_name);
				c_sc.addChild(person);
				s = "组织："+organization.getName()+"\n";
				print(organization, space);
			}
		}
		output.setText(s);
    }

    @FXML
    void setTeam() {
    	c_sc = null;
		p_sc = organization;
		s_sc = null;
    	s = null;
    	ArrayList<SafeComponent> chilren = organization.getChildren();
		String p_name = t_in2.getText();
		if(p_name.compareTo(organization.getName()) == 0) {
			String t_name = t_in.getText();
			SafeComposite team = new SafeComposite();
			team.setName(t_name);
			organization.addChild(team);
			s = "组织："+organization.getName()+"\n";
			print(organization, space);
		}
		else {
			check(organization, p_name);
			if(c_sc == null) {
				Alert al = new Alert(AlertType.WARNING);
	    		al.setTitle("WARNING");
	    		al.setHeaderText(null);
	    		al.setContentText("请输入正确的组织或团队名称作为父亲节点！");
	    		al.showAndWait();
			}
			else {
				String t_name = t_in.getText();
				SafeComposite team = new SafeComposite();
				team.setName(t_name);
				c_sc.addChild(team);
				s = "组织："+organization.getName()+"\n";
				print(organization, space);
			}
		}
		output.setText(s);

    }

    @FXML
    void delTeam() {
    	c_sc = null;
		p_sc = organization;
		s_sc = null;
    	s = null;
    	ArrayList<SafeComponent> chilren = organization.getChildren();
		String t_name = t_in.getText();
		check2(organization, t_name);
		if(c_sc == null) {
			Alert al = new Alert(AlertType.WARNING);
    		al.setTitle("WARNING");
    		al.setHeaderText(null);
    		al.setContentText("该团队不存在！");
    		al.showAndWait();
		}
		
		p_sc.removeChild(c_sc);		
		s = "组织："+organization.getName()+"\n";
		print(organization, space);
		output.setText(s);
    }

    @FXML
    void delPerson() {
    	c_sc = null;
		p_sc = organization;
		s_sc = null;
    	s = null;
    	ArrayList<SafeComponent> chilren = organization.getChildren();
		String p_name = p_in.getText();
		check2(organization, p_name);
		if(s_sc == null) {
			Alert al = new Alert(AlertType.WARNING);
    		al.setTitle("WARNING");
    		al.setHeaderText(null);
    		al.setContentText("该个人不存在！");
    		al.showAndWait();
		}
		
		p_sc.removeChild(s_sc);		
		s = "组织："+organization.getName()+"\n";
		print(organization, space);
		output.setText(s);
    }

}